#!/usr/bin/env python
# time a function using time.time() and the a @ function decorator
import time
import sys 

def print_timing(func, stream=sys.stderr):
    def wrapper(*arg):
        start_time = time.time()
        res = func(*arg)
        end_time = time.time()
        stream.write('%s took %0.3f ms\n' % (func.func_name, (end_time-start_time)*1000.0))
        return res
    return wrapper

# declare the @ decorator just before the function, invokes print_timing()
@print_timing
def test(n):
    """ returns a list of prime numbers from 2 to < n using a sieve algorithm"""
    for i in range(n):
        time.sleep(0.5)

if __name__ == "__main__":
    print "test timed function"
    test(5)
    test(10)
    
"""
test timed function
test took 2499.641 ms
test took 5000.000 ms
"""
